
let Builder = function () { // 建造者-构建表单对象
  let finalObj = {}; // 最终表单对象
  let BuildOrderingUnit = function () {}
  BuildOrderingUnit.prototype.set = function (part) { // 组装器
    let commontProps = {};
    let partProps = {}
    for (let key in part) { // 分离组件独立属性和公共属性
      if (typeof part[key] === 'object') {
        partProps[key] = part[key]
      } else if (typeof part[key] !== 'function') {
        commontProps[key] = part[key]
      }
    }	
    for (let key in partProps) {
      finalObj[key] = Object.assign({}, commontProps, partProps[key]); // 公共属性和组件独立属性合并并且如果有一样的key，私有属性的优先级更高
      if (commontProps.readonly) { // 优先级例外：查看表单时没有必填提示
        finalObj[key].required = false
      }
    }
  }
  for (let i = 0; i < arguments.length; i++) {
    if (typeof arguments[i] === 'object') {
      BuildOrderingUnit.prototype.set(arguments[i])
    }
  }
  // console.log(JSON.stringify(finalObj))
  return finalObj
}
/**
 * [copy 通过递归实现对象深克隆的方法]
 * @param  {[type]} aObject [原对象]
 * @return {[type]}         [克隆后的对象]
 */
function copy (aObject) {
  if (!aObject) {
    return aObject
  }
  let bObject, v, k
  bObject = Array.isArray(aObject) ? [] : {}
  for (k in aObject) {
    v = aObject[k]
    bObject[k] = typeof v === 'object' ? copy(v) : v
  }
  return bObject
}
/**
 * @param  {[type]} 日期类型
 */
let getDefaultDate = function(type) {
  const date = new Date();
  if (type === 'end+1') { // 如果时数字， 那么就从当前时间回溯
    date.setFullYear(date.getFullYear()+1);
  }
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  if (type === 'start') {
    year = year - 120;
  }else if (!isNaN(type)) { // 如果时数字， 那么就从当前时间回溯
    year = year - type;
  }
  month = month > 9 ? month : '0' + month;;
  day = day > 9 ? day : '0' + day;
  return `${year}-${month}-${day}`;
}
const fsm = uni.getFileSystemManager();
const FILE_BASE_NAME = 'tmp_base64src';

const base64src = function(base64data) {
  return new Promise((resolve, reject) => {
    const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
    // console.log(format, bodyData);
    if (!format) {
      reject(new Error('ERROR_BASE64SRC_PARSE'));
    }
    const filePath = `${uni.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`;
    const buffer = uni.base64ToArrayBuffer(bodyData);
    fsm.writeFile({
      filePath,
      data: buffer,
      encoding: 'binary',
      success() {
        resolve(filePath);
      },
      fail() {
        reject(new Error('ERROR_BASE64SRC_WRITE'));
      },
    });
  });
};
const objConvertUrl = function(url = '', params = {}) {
	if(params && typeof params === 'object') {
		var args = []
		Object.keys(params).forEach((key) => {
			let value = params[key]
			value = value.replace(/\?/g, '##')
			value = value.replace(/=/g, '@@')
			value = value.replace(/&/g, '✈✈')
			args.push(key + "=" + value)
		})
		if(args.length) {
			if(url) {
				url += '?' + args.join('&')
			} else {
				url = args.join('&')
			}
		}
	}
	return url
}
const util = {
  Builder,
  copy,
  getDefaultDate,
  base64src,
	objConvertUrl
}

export default util
